﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PTUDW_CNTN2010_F_CDT.Models;

namespace PTUDW_CNTN2010_F_CDT.Controllers
{
    public class RatingController : Controller
    {
        EcommerceDBContext db = new EcommerceDBContext();
        
        [HttpPost]
        public int PostRating(int rating, int productId)
        {
            if (Request.Cookies["rating" + productId] != null)
                return 0;
            Response.Cookies["rating" + productId].Value = DateTime.Now.ToString();
            Response.Cookies["rating" + productId].Expires = DateTime.Now.AddYears(1);
            var p = db.Products.Single(pp => pp.ProductId == productId);
            p.NumRating++;
            p.Rating = (p.Rating * (p.NumRating - 1) + rating) / (1.0f * p.NumRating);
            db.SaveChanges();
            return 1;
        }

    }
}
